{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:28:45.554825400Z",
     "start_time": "2024-07-15T01:28:45.157255700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Series\n",
    "1.Series介绍\n",
    "2.Series创建"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "3    4\n",
      "4    5\n",
      "dtype: int64\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "# 通过list创建\n",
    "s1 = pd.Series([1, 2, 3, 4, 5])\n",
    "print(s1)\n",
    "print(type(s1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-13T03:11:12.377608400Z",
     "start_time": "2024-07-13T03:11:12.374093500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "# 2.通过数组创建\n",
    "arr1 = np.arange(1, 6)\n",
    "print(arr1)\n",
    "print(type(arr1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:34:17.251304700Z",
     "start_time": "2024-07-15T01:34:17.244791200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "0    1\n1    2\n2    3\n3    4\n4    5\ndtype: int64"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = pd.Series(arr1)\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:34:19.187722300Z",
     "start_time": "2024-07-15T01:34:19.181373200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\nd    4\ne    5\ndtype: int64"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引数量不够会报错，过多也会报错\n",
    "s2 = pd.Series(arr1, index=['a', 'b', 'c', 'd', 'e'])\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:34:21.098423900Z",
     "start_time": "2024-07-15T01:34:21.091850900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "print(s1.values)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-13T03:18:57.978231400Z",
     "start_time": "2024-07-13T03:18:57.976226100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "RangeIndex(start=0, stop=5, step=1)"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-13T03:19:20.514758500Z",
     "start_time": "2024-07-13T03:19:20.513753700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name     Alan\n",
      "age        26\n",
      "class      三班\n",
      "sex       NaN\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 3.通过字典来创建\n",
    "dict_1 = {'name': 'Alan', 'age': '26', 'class': '三班'}\n",
    "# 用字典创建，多一个索引不会报错\n",
    "s3 = pd.Series(dict_1, index=['name', 'age', 'class', 'sex'])\n",
    "print(s3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:29:11.074830300Z",
     "start_time": "2024-07-15T01:29:11.028463300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "三.Series的基本用法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "name     False\nage      False\nclass    False\nsex       True\ndtype: bool"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.1 isnull和notnull检查缺失值\n",
    "s3.isnull()  # 判断是否为空 空就是True"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-13T03:37:54.327147600Z",
     "start_time": "2024-07-13T03:37:54.324639300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "name      True\nage       True\nclass     True\nsex      False\ndtype: bool"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3.notnull()  # 判断是否否不为空，非空值为True"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-13T03:38:39.371328200Z",
     "start_time": "2024-07-13T03:38:39.369324Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['name', 'age', 'class', 'sex'], dtype='object')\n",
      "['Alan' '26' '三班' nan]\n"
     ]
    }
   ],
   "source": [
    "# 3.2 通过索引获取数据\n",
    "print(s3.index)\n",
    "print(s3.values)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-13T03:42:48.340308200Z",
     "start_time": "2024-07-13T03:42:48.333827600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alan\n"
     ]
    }
   ],
   "source": [
    "# 下表取数\n",
    "print(s3.iloc[0])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-13T09:18:51.612280500Z",
     "start_time": "2024-07-13T09:18:51.609254100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "'26'"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标签名\n",
    "s3['age']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-13T03:46:05.216120800Z",
     "start_time": "2024-07-13T03:46:05.214114900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age     26\n",
      "sex    NaN\n",
      "dtype: object\n",
      "name    Alan\n",
      "age       26\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 选取多个\n",
    "print(s3.iloc[[1,3]])\n",
    "print(s3[['name','age']])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:31:17.678081800Z",
     "start_time": "2024-07-15T01:31:17.675813500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "age      26\nclass    三班\ndtype: object"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "s3[1:3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:32:09.229690300Z",
     "start_time": "2024-07-15T01:32:09.208646500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "name     Alan\nage        26\nclass      三班\ndtype: object"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3['name':'class'] # 标签切片，包含末端数据"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:33:06.967776400Z",
     "start_time": "2024-07-15T01:33:06.961774200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "d    4\ne    5\ndtype: int64"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 布尔索引\n",
    "s2[s2>3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:35:03.991379Z",
     "start_time": "2024-07-15T01:35:03.983864700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a     2\n",
      "b     4\n",
      "c     6\n",
      "d     8\n",
      "e    10\n",
      "dtype: int64\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 3.3 索引与数据的对应关系不被运算结果影响\n",
    "print(s2*2)\n",
    "print(s2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:36:24.807792Z",
     "start_time": "2024-07-15T01:36:24.789632600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "Name: tmep, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 3.4 name属性\n",
    "s2.name='tmep' # 对象名\n",
    "s2.index.name='year' # 对象索引名\n",
    "print(s2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:37:53.703941900Z",
     "start_time": "2024-07-15T01:37:53.697943700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "year\na    1\nb    2\nc    3\nName: tmep, dtype: int64"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.head(3) # 默认前五行"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:39:57.115175Z",
     "start_time": "2024-07-15T01:39:57.107302700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "year\nc    3\nd    4\ne    5\nName: tmep, dtype: int64"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.tail(3) # 默认后五行"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T01:40:24.947332Z",
     "start_time": "2024-07-15T01:40:24.940801800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
